package edu.webtech.project.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import edu.webtech.project.client.UserAccountService;
import edu.webtech.project.client.UserAccountServiceAsync;

public class RegisterPage extends Composite {

	private static RegisterPageUiBinder uiBinder = GWT
			.create(RegisterPageUiBinder.class);

	interface RegisterPageUiBinder extends UiBinder<Widget, RegisterPage> {
	}
	
	private final UserAccountServiceAsync userAccountService = GWT
			.create(UserAccountService.class);

	@UiField Button btnRegister;
	@UiField TextBox txtFirstName;
	@UiField TextBox txtLastName;
	@UiField TextBox txtUserName;
	@UiField PasswordTextBox txtPassword;
	@UiField PasswordTextBox txtPassConfirm;
	@UiField TextBox txtEmail;
	@UiField Button btnSignin;
	@UiField Label userNameError;
	@UiField Label passwordError;
	@UiField Label passwordConfError;
	@UiField Label emailError;
	@UiField Label nameError;
	
	
	private String strFirstName;
	private String strLastName;
	private String strUserName;
	private String strPassword;
	private String strConfPass;
	private String strEmailAdd;
	
	private MainLayout mainLayout;
	
	public RegisterPage(MainLayout o) {
		initWidget(uiBinder.createAndBindUi(this));
		mainLayout = o;
	}
	
	public void updateFromForm() {
		this.strFirstName = txtFirstName.getText().trim();
		this.strLastName  = txtLastName.getText().trim();
		this.strUserName  = txtUserName.getText().trim();
		this.strPassword  = txtPassword.getText().trim();
		this.strConfPass  = txtPassConfirm.getText().trim();
		this.strEmailAdd  = txtEmail.getText().trim();
	}
	
	public int Validate() {
		nameError.setText("");
		userNameError.setText("");
		passwordError.setText("");
		passwordConfError.setText("");
		emailError.setText("");
		
		String USERNAME_PATTERN = "^[a-zA-Z]+[-_.]?[a-zA-Z0-9]+$";
		String PASSWORD_PATTERN = "^[a-zA-Z0-9]+$";
		String EMAIL_PATTERN    = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
		
		if (true == strFirstName.isEmpty() || true == strLastName.isEmpty()) {
			nameError.setText("You can't leave them empty.");
			return 1;
		}
		
		if (true == strUserName.isEmpty()) {
			userNameError.setText("Please enter a username.");
			return 1;
		} else if (strUserName.length() < 4) {
			userNameError.setText("Minimum length is 4 characters.");
			return 1;
		} else if (false == strUserName.matches(USERNAME_PATTERN)) {
			userNameError.setText("You can use letters numbers and periods.");
			return 1;
		}
		
		if (true == strPassword.isEmpty()) {
			passwordError.setText("Please enter a password");
			return 1;
		} else if (strPassword.length() < 4) {
			passwordError.setText("Minimum length is 4 characters.");
			return 1;
		} else if (false == strPassword.matches(PASSWORD_PATTERN)) {
			passwordError.setText("You can use letters numbers and periods.");
			return 1;
		}
		
		if (true == strConfPass.isEmpty()) {
			passwordConfError.setText("Please confirm your password");
			return 1;
		} else if (0 != strConfPass.compareTo(strPassword)) {
			passwordConfError.setText("Passwords do not match.");
			return 1;
		}
		
		if (true == strEmailAdd.isEmpty()) {
			emailError.setText("Please enter your email address");
			return 1;
		} else if (false == strEmailAdd.matches(EMAIL_PATTERN)) {
			emailError.setText("Invalid email address.");
			return 1;
		}
		
		return 0;
	}
	
	public void register() {
		userAccountService.register(strFirstName, strLastName, strUserName,
				strPassword, strEmailAdd, new AsyncCallback<Void>() {
					public void onFailure(Throwable caught) {
						Window.alert("Damn !! What a shame. An error has occurred.");
					}

					@Override
					public void onSuccess(Void result) {
						Window.alert("User has been successfully registered.");
						mainLayout.getContentPanel().clear();
						mainLayout.getContentPanel().add(new LoginPage(mainLayout));
					}
				});
	}
	
	
	@UiHandler("btnRegister")
	public void onBtnRegisterClick(ClickEvent e) {
		
		updateFromForm();
		if (0 == Validate())
			register();
		
		
	}
	
	@UiHandler("btnSignin")
	public void onBtnSigninClick(ClickEvent e) {
		mainLayout.getContentPanel().clear();
		mainLayout.getContentPanel().add(new LoginPage(mainLayout));
	}
	
}
